package cn.ybk.org.controller;

import cn.ybk.basic.annotation.PreAuthorize;
import cn.ybk.basic.jwt.UserInfo;
import cn.ybk.basic.util.AjaxResult;
import cn.ybk.basic.util.LoginContext;
import cn.ybk.basic.util.PageList;
import cn.ybk.org.domain.Department;
import cn.ybk.org.domain.Employee;
import cn.ybk.org.domain.Shop;
import cn.ybk.org.query.EmployeeQuery;
import cn.ybk.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // 相当于 @Controller+@ResponseBody 每个public中都相当于添加上了@ResponBody
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService EmployeeService;

    @GetMapping
    public List<Employee> findAll() {
        return EmployeeService.findAll();
    }

    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return EmployeeService.findById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        Long id = employee.getId();
        if (id == null) {
            EmployeeService.add(employee);
        } else {
            EmployeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        EmployeeService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping
    public AjaxResult batch(@RequestBody List<Long> ids) {
        EmployeeService.batch(ids);
        return AjaxResult.me().success();
    }


    @PostMapping
    @PreAuthorize(name = "查询全部数据", sn = "employee:list")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
        Shop shop = LoginContext.getUserInfo().getShop();
        if (shop.getId() != null) {
            query.setShopId(shop.getId());
        }
        return EmployeeService.queryByPage(query);
    }


    @GetMapping("/drawer/{id}")
    public  List<Employee> drawer(@PathVariable("id") Long id){
        return EmployeeService.drawer(id);
    }


    @GetMapping("/list/{shopId}")
    public  List<Employee> list(@PathVariable("shopId") Long id){
        return EmployeeService.list(id);
    }

}
